package com.forezp.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * springboot-jdbc-DruidConfiguration
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/16 13:35
 */
@Configuration
public class DruidConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);

    @Autowired
    private DruidProperty druidProperty;

    @Bean
    public ServletRegistrationBean druidServlet() {
        logger.info("init Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单
        servletRegistrationBean.addInitParameter("allow", "*");
        // IP黑名单(共同存在时，deny优先于allow)
        servletRegistrationBean.addInitParameter("deny", "*");
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

    //解决 spring.datasource.filters=stat,wall,log4j 无法正常注册进去


    @Bean     //声明其为Bean实例
    @Primary  //在同样的DataSource中，首先使用被标注的DataSource
    public DataSource dataSource() {
        return createDataSource(druidProperty);
    }

    public static DruidDataSource createDataSource(DruidProperty druidProperty) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(druidProperty.url);
        datasource.setUsername(druidProperty.username);
        datasource.setPassword(druidProperty.password);
        datasource.setDriverClassName(druidProperty.driverClassName);

        //configuration
        datasource.setInitialSize(druidProperty.initialSize);
        datasource.setMinIdle(druidProperty.minIdle);
        datasource.setMaxActive(druidProperty.maxActive);
        datasource.setMaxWait(druidProperty.maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(druidProperty.timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(druidProperty.minEvictableIdleTimeMillis);
        datasource.setValidationQuery(druidProperty.validationQuery);
        datasource.setTestWhileIdle(druidProperty.testWhileIdle);
        datasource.setTestOnBorrow(druidProperty.testOnBorrow);
        datasource.setTestOnReturn(druidProperty.testOnReturn);
        datasource.setPoolPreparedStatements(druidProperty.poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(druidProperty.maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(druidProperty.filters);
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
        }
        datasource.setConnectionProperties(druidProperty.connectionProperties);
        return datasource;
    }


}